package org.snare.alumni.modules.app;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.snare.spider.cobweb.oss.dto.FileUploadDTO;
import org.snare.spider.cobweb.oss.service.api.OssService;
import org.snare.spider.cobweb.third.tencent.weixin.offiaccount.webapps.WebPageAuth;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "页面")
@RequiredArgsConstructor
@Controller
@RequestMapping("/alumni/app")
public class PageController {

    @Value("${tencent.wechat.appId}")
    private String appId;

    @Value("${tencent.wechat.appSecret}")
    private String appSecret;

//    @Value("${tencent.wechat.appSecret}")
//    private String homePage;

    private final OssService ossService;

    /**
     * http://alumni.snare.org.cn:9002/alumni/app/page/index?url=http://alumni.snare.org.cn:8001/pages/transfer/
     *
     * @param url
     * @param response
     */
    @GetMapping("/page/index")
    public void index(String url, HttpServletResponse response) {
        String homePage = "http://alumni.snare.org.cn/pages/transfer/";
        WebPageAuth webPageAuth = new WebPageAuth(appId, appSecret);
        if (StringUtils.isEmpty(url)) {
            url = homePage;
        }
        webPageAuth.authorize(url, response);
    }

    @ApiOperation(value = "uploadFile", httpMethod = "POST")
    @ResponseBody
    @PostMapping("/uploadFile")
    public Map<String, Object> uploadFile(MultipartFile file) {
//        Map<String, Object> map = new HashMap<>();
        FileUploadDTO fileUploadDTO = ossService.upload(file, null);
//        map.put("id", fileUploadDTO.getFileId());
//        map.put("name", fileUploadDTO.getFileName());
//        map.put("url", fileUploadDTO.getPreviewUrl());
        HashMap<String, Object> result = new HashMap<>();
        result.put("data", fileUploadDTO);
        return result;
    }

}
